package com.wjm.myhookframework;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class HookUtil {
    private Context context;

    public void hookStartActivity(Context context) {
        this.context = context;
        try {
            Log.d("TAG","hookStartActivity 1");
            Class<?> ActivityManagerNativecls = Class.forName("android.app.ActivityManagerNative");
            Field gDefault = ActivityManagerNativecls.getDeclaredField("gDefault");
            gDefault.setAccessible(true);
            //            因为是静态变量  所以获取的到的是系统值  hook   伪hook
            Object defaltValue = gDefault.get(null);
            Class<?> singletonClass = Class.forName("android.util.Singleton");
            Field mInstance = singletonClass.getDeclaredField("mInstance");
            mInstance.setAccessible(true);
            Object iActivityManagerObject = mInstance.get(defaltValue);
            Class<?> IActivityManagerIntercept = Class.forName("android.app.IActivityManager");
            StartActivty startActivty = new StartActivty(iActivityManagerObject);
            Object oldIactivityManager = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{IActivityManagerIntercept, View.OnClickListener.class}, startActivty);
            mInstance.set(defaltValue, oldIactivityManager);
            Log.d("TAG","hookStartActivity 2");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    class StartActivty implements InvocationHandler {

        private Object iActivityManagerObject;

        public StartActivty(Object iActivityManagerObject) {
            this.iActivityManagerObject = iActivityManagerObject;
        }

        @Override
        public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
            if ("startActivity".equals(method.getName())) {
                Log.d("TAG","StartActivty invoke if");
                Intent intent = null;
                int index = 0;
                for (int i = 0; i < objects.length; i++) {
                    Object object = objects[i];
                    if (object instanceof Intent) {
                        intent = (Intent) objects[i];
                        index = i;
                    }
                }
                Intent newIntent = new Intent();
                ComponentName componentName = new ComponentName(context, ProxyActivity.class);
                newIntent.setComponent(componentName);
                newIntent.putExtra("oldIntent", intent);
                objects[index] = newIntent;
            }
            Log.d("TAG","StartActivty invoke");
            return method.invoke(iActivityManagerObject, objects);
        }
    }

    public void hookHookMh(Context context) {
        try {
            Log.d("TAG","hookHookMh 1");
            Class<?> aClass = Class.forName("android.app.ActivityThread");
            Field sCurrentActivityThread = aClass.getDeclaredField("sCurrentActivityThread");
            sCurrentActivityThread.setAccessible(true);
            Object activityThreadObj = sCurrentActivityThread.get(null);
            Field handlerField = aClass.getDeclaredField("mH");
            handlerField.setAccessible(true);
            Handler mH = (Handler) handlerField.get(activityThreadObj);
            Field mCallback = Handler.class.getDeclaredField("mCallback");
            mCallback.setAccessible(true);
            mCallback.set(mH,new ActivityMH(mH));
            Log.d("TAG","hookHookMh 2");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    class ActivityMH implements Handler.Callback{
        private Handler mH;

        public ActivityMH(Handler mH) {
            this.mH=mH;
        }

        @Override
        public boolean handleMessage(Message message) {
            if(message.what ==100){
                handleLuachActivity(message);
            }
            mH.handleMessage(message);
            return true;
        }

        private void handleLuachActivity(Message message) {
            Object obj = message.obj;
            try {
                Field intent = obj.getClass().getDeclaredField("intent");
                intent.setAccessible(true);
                Intent realyIntent = (Intent) intent.get(obj);
                Intent oldIntent = realyIntent.getParcelableExtra("oldIntent");
                if(null!=oldIntent){
                    SharedPreferences share = context.getSharedPreferences("david", Context.MODE_PRIVATE);

                    // 集中式登录
                    if(share.getBoolean("login",false)){
//                    if(share.getBoolean("login",false)||oldIntent.getComponent().getClassName().equals(SceondActivity.class.getName())){
                        Log.d("TAG","true 登录了");
                        realyIntent.setComponent(oldIntent.getComponent());
                    }else{
                        Log.d("TAG","false 还未登录");
                        ComponentName componentName = new ComponentName(context, LoginActivity.class);
                        realyIntent.putExtra("extraIntent",oldIntent.getComponent().getClassName());
                        realyIntent.setComponent(componentName);
                    }

                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}
